
package com.zoo.transfer

import groovy.sql.Sql

import com.mongodb.MongoClient
import com.mongodb.BasicDBObject
import com.gmongo.GMongo
//import com.mongodb.util.JSON
//import com.mongodb.ServerAddress

class Execute {

    def sql, gmongo, conv
    def databaseService = new DatabaseService()

    def init() {
        sql = Sql.newInstance("jdbc:mysql://192.168.1.200:3306/sklad", "vasil", "rupic", "com.mysql.jdbc.Driver")
        //def addrs = [new ServerAddress("192.168.1.200" , 30001), new ServerAddress("192.168.1.201" , 30002),new ServerAddress("192.168.1.202" , 30003)]
        //db = new MongoClient("192.168.1.200" , 30001).getDB("store")
        gmongo = new GMongo("192.168.1.200:30001")

        conv = new Convert()
    }

    def testMysql() {
        sql.eachRow("select count(*) cnt from sklad.items") {
            println "Count: ${it.cnt}"
        }
    }

    def testMongoDB() {

        def table = db.getCollection("user")

        table.drop()

	table.insert(JSON.parse('''{name: 'Vasill', age: 52, created: '2013-05-30 15:33:00'}''') as BasicDBObject)

	def cursor = table.find([name: "Vasill"] as BasicDBObject)
	while(cursor.hasNext()) {
            println cursor.next()
	}
    }

    def customTableMove() {

        def collect = db.getCollection(Customer.COLLECTION_NAME), pageSize = 100
        collect.drop()
        collect.ensureIndex([cid: 1] as BasicDBObject) // make index

        def result
        for(page in 0..100000) {
            result = conv.customerTableJSON(sql, collect, page, pageSize)
            println result
            if(result.custcount == 0) break
        }
        sql.close()
    }

    def itemsTableMove() {

        def db = databaseService.db(), pageSize = 1
        db.items.drop()
        db.categories.drop()
        //collect.ensureIndex([cid: 1] as BasicDBObject) // make index

        def result
//        for(page in 0..100000) {
            result = conv.itemsTableJSON(sql, db, 0, pageSize)
            //println result
 //           if(result.custcount == 0) break
  //      }
        sql.close()
    }

}

def exec = new Execute()

println "---------------"

exec.init()
//exec.testMysql()
//exec.testMongoDB()
//exec.customTableMove()
exec.itemsTableMove()

println "---------------"
